import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.*;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;

public class HDFSApp1 {
    Configuration configuration;
    URI uri;
    FileSystem fileSystem;

    @BeforeClass
    public static void begin() {
        System.out.println("begin");
    }

    @AfterClass
    public static void stop() {
        System.out.println("stop");
    }

    @Before
    public void setUp() {
        System.out.println("setUp");
        configuration = new Configuration();
        configuration.set("dfs.replication", "1");
        try {
            uri = new URI("hdfs://bdptest1:8020");
            fileSystem = FileSystem.get(uri, configuration, "hadoop");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @After
    public void tearDown() {
        System.out.println("tearDown");
        try {
            fileSystem.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test01() throws Exception {
        fileSystem.mkdirs(new Path("/root/test2"));
    }

    @Test
    public void test0010() throws Exception {
        //FSDataOutputStream out = fileSystem.create(new Path("/g5/test/ruozedata-hadoop.txt"));
        //FSDataOutputStream out = fileSystem.append(new Path("/root/test2/TestUser.txt"));
        boolean exists = fileSystem.exists(new Path("/root/test2/TestUser.txt"));
        FSDataOutputStream out = null;
        //  if (!exists) {
        out = fileSystem.create(new Path("/root/test2/TestUser.txt"));
        // }
        StringBuilder sb = new StringBuilder();
        String aa = null;
        for (int i = 1; i < 30; i++) {
            sb.append(1 + ",");
            sb.append("name_" + i + ",");
            sb.append(i + 10 + ",");
            sb.append("天府 " + i + " 街" + ",");
            sb.append(1858246338 + i + "\n");
        }
        out.write(sb.toString().getBytes());
//        out.writeUTF("WG \n");
//        out.writeUTF("wx_tv \n");
        out.flush();
        out.close();
    }

    @Test
    public void test03() throws Exception {
        FSDataInputStream in = fileSystem.open(new Path("/root/test2/wc.txt"));
        IOUtils.copyBytes(in, System.out, 1024);
    }

    @Test
    public void test04() throws Exception {
//        fileSystem.rename(new Path("/g5/test/ruozedata-hadoop.txt"),
//                new Path("/g5/test/ruozedata-hadoop.txt-bak"));
        String inputPath = "C:\\\\Users\\\\admin\\\\IdeaProjects\\\\bdp\\\\trunk\\\\device-log-parent" +
                "\\\\writer\\\\hive-writer\\\\src\\\\main\\\\scala\\\\com\\\\zetatech\\\\bdp\\\\writer\\\\hive\\\\wc.txt";

        // 获取写入流
        PrintWriter writer = new PrintWriter(new File(inputPath));

        //写入文件
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i < 30; i++) {
            sb.append(i + ",");
            sb.append("name_" + i + ",");
            sb.append(i + 10 + ",");
            sb.append("天府 " + i + " 街" + ",");
            sb.append(1858246338 + i + "\n");
        }
        writer.write(String.valueOf(sb.toString().getBytes()));
        writer.flush();
        writer.close();
        // local ==> hdfs
        //  fileSystem.copyFromLocalFile(new Path(inputPath), new Path("/root/test2/TestUser.txt"));

//        // hdfs ==> local
//        fileSystem.copyToLocalFile(new Path(""), new Path(""));
//
//        fileSystem.listFiles(new Path(""), true);
//
//        fileSystem.delete(new Path(""), true);


    }

    @Test
    public void test010() throws Exception {

        StringBuilder str = new StringBuilder();
        str.append("asddsgsd|");
        System.out.println(str);
        str.deleteCharAt(str.lastIndexOf("|"));
        System.out.println(str);
        StringBuilder delete = str.delete(0, str.length());
        System.out.println(delete);
        System.out.println(str.length());
    }


}

